<template>
  <el-dialog
    v-model="visible"
    :title="dialogTitle"
    width="900px"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <div class="fund-detail">
      <!-- 预算详情 -->
      <template v-if="detailType === 'budget'">
        <div class="detail-section">
          <h3 class="section-title">基本信息</h3>
          <el-row :gutter="20" class="detail-row">
            <el-col :span="8">
              <div class="detail-item">
                <span class="label">预算年度：</span>
                <span class="value">{{ fundData?.year }}</span>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="detail-item">
                <span class="label">预算类型：</span>
                <el-tag :type="getBudgetTypeTag(fundData?.type)">
                  {{ getBudgetTypeText(fundData?.type) }}
                </el-tag>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="detail-item">
                <span class="label">预算状态：</span>
                <el-tag :type="getBudgetStatusTag(fundData?.status)">
                  {{ getBudgetStatusText(fundData?.status) }}
                </el-tag>
              </div>
            </el-col>
          </el-row>
          
          <el-row :gutter="20" class="detail-row">
            <el-col :span="12">
              <div class="detail-item">
                <span class="label">预算名称：</span>
                <span class="value">{{ fundData?.name }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="detail-item">
                <span class="label">预算总额：</span>
                <span class="value amount">¥{{ formatAmount(fundData?.totalAmount) }}</span>
              </div>
            </el-col>
          </el-row>
          
          <div class="detail-item full-width">
            <span class="label">预算说明：</span>
            <div class="value description">{{ fundData?.description || '暂无说明' }}</div>
          </div>
        </div>

        <div class="detail-section">
          <h3 class="section-title">预算明细</h3>
          <el-table :data="fundData?.details || []" border style="width: 100%">
            <el-table-column prop="item" label="预算项目" width="200" />
            <el-table-column prop="amount" label="预算金额" width="150">
              <template #default="{ row }">
                <span class="amount">¥{{ formatAmount(row.amount) }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="description" label="说明" />
          </el-table>
          
          <div class="summary-info">
            <el-row :gutter="20">
              <el-col :span="8">
                <div class="summary-item">
                  <span class="summary-label">明细总计：</span>
                  <span class="summary-value amount">¥{{ formatAmount(detailTotal) }}</span>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="summary-item">
                  <span class="summary-label">已使用：</span>
                  <span class="summary-value amount">¥{{ formatAmount(fundData?.usedAmount || 0) }}</span>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="summary-item">
                  <span class="summary-label">剩余预算：</span>
                  <span class="summary-value amount">¥{{ formatAmount((fundData?.totalAmount || 0) - (fundData?.usedAmount || 0)) }}</span>
                </div>
              </el-col>
            </el-row>
          </div>
        </div>
      </template>

      <!-- 收入详情 -->
      <template v-if="detailType === 'income'">
        <div class="detail-section">
          <h3 class="section-title">收入信息</h3>
          <el-row :gutter="20" class="detail-row">
            <el-col :span="8">
              <div class="detail-item">
                <span class="label">收入日期：</span>
                <span class="value">{{ fundData?.date }}</span>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="detail-item">
                <span class="label">收入类型：</span>
                <el-tag :type="getIncomeTypeTag(fundData?.type)">
                  {{ getIncomeTypeText(fundData?.type) }}
                </el-tag>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="detail-item">
                <span class="label">收入金额：</span>
                <span class="value amount">¥{{ formatAmount(fundData?.amount) }}</span>
              </div>
            </el-col>
          </el-row>
          
          <el-row :gutter="20" class="detail-row">
            <el-col :span="12">
              <div class="detail-item">
                <span class="label">收入来源：</span>
                <span class="value">{{ fundData?.source }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="detail-item">
                <span class="label">操作人：</span>
                <span class="value">{{ fundData?.operator }}</span>
              </div>
            </el-col>
          </el-row>
          
          <el-row :gutter="20" class="detail-row">
            <el-col :span="12">
              <div class="detail-item">
                <span class="label">发票号码：</span>
                <span class="value">{{ fundData?.invoiceNumber || '暂无' }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="detail-item">
                <span class="label">创建时间：</span>
                <span class="value">{{ fundData?.createTime }}</span>
              </div>
            </el-col>
          </el-row>
          
          <div class="detail-item full-width">
            <span class="label">收入说明：</span>
            <div class="value description">{{ fundData?.description || '暂无说明' }}</div>
          </div>
        </div>

        <div class="detail-section" v-if="fundData?.attachments?.length">
          <h3 class="section-title">相关附件</h3>
          <div class="attachment-list">
            <div
              v-for="(file, index) in fundData.attachments"
              :key="index"
              class="attachment-item"
            >
              <el-icon class="file-icon"><Document /></el-icon>
              <span class="file-name">{{ file.name }}</span>
              <el-button type="primary" link @click="downloadFile(file)">
                下载
              </el-button>
            </div>
          </div>
        </div>
      </template>

      <!-- 支出详情 -->
      <template v-if="detailType === 'expense'">
        <div class="detail-section">
          <h3 class="section-title">支出信息</h3>
          <el-row :gutter="20" class="detail-row">
            <el-col :span="8">
              <div class="detail-item">
                <span class="label">支出日期：</span>
                <span class="value">{{ fundData?.date }}</span>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="detail-item">
                <span class="label">支出类型：</span>
                <el-tag :type="getExpenseTypeTag(fundData?.type)">
                  {{ getExpenseTypeText(fundData?.type) }}
                </el-tag>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="detail-item">
                <span class="label">支出金额：</span>
                <span class="value amount">¥{{ formatAmount(fundData?.amount) }}</span>
              </div>
            </el-col>
          </el-row>
          
          <el-row :gutter="20" class="detail-row">
            <el-col :span="12">
              <div class="detail-item">
                <span class="label">支出用途：</span>
                <span class="value">{{ fundData?.purpose }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="detail-item">
                <span class="label">预算科目：</span>
                <span class="value">{{ getBudgetSubjectText(fundData?.budgetSubject) }}</span>
              </div>
            </el-col>
          </el-row>
          
          <el-row :gutter="20" class="detail-row">
            <el-col :span="8">
              <div class="detail-item">
                <span class="label">申请人：</span>
                <span class="value">{{ fundData?.applicant }}</span>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="detail-item">
                <span class="label">审批状态：</span>
                <el-tag :type="getApprovalStatusTag(fundData?.approvalStatus)">
                  {{ getApprovalStatusText(fundData?.approvalStatus) }}
                </el-tag>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="detail-item">
                <span class="label">创建时间：</span>
                <span class="value">{{ fundData?.createTime }}</span>
              </div>
            </el-col>
          </el-row>
          
          <div class="detail-item full-width">
            <span class="label">支出说明：</span>
            <div class="value description">{{ fundData?.description || '暂无说明' }}</div>
          </div>
        </div>

        <div class="detail-section" v-if="fundData?.vouchers?.length">
          <h3 class="section-title">支出凭证</h3>
          <div class="attachment-list">
            <div
              v-for="(file, index) in fundData.vouchers"
              :key="index"
              class="attachment-item"
            >
              <el-icon class="file-icon"><Document /></el-icon>
              <span class="file-name">{{ file.name }}</span>
              <el-button type="primary" link @click="downloadFile(file)">
                下载
              </el-button>
            </div>
          </div>
        </div>

        <div class="detail-section" v-if="fundData?.approvalHistory?.length">
          <h3 class="section-title">审批历史</h3>
          <el-timeline>
            <el-timeline-item
              v-for="(item, index) in fundData.approvalHistory"
              :key="index"
              :timestamp="item.time"
              placement="top"
            >
              <div class="approval-item">
                <div class="approval-header">
                  <span class="approver">{{ item.approver }}</span>
                  <el-tag :type="getApprovalStatusTag(item.status)" size="small">
                    {{ getApprovalStatusText(item.status) }}
                  </el-tag>
                </div>
                <div class="approval-comment" v-if="item.comment">
                  {{ item.comment }}
                </div>
              </div>
            </el-timeline-item>
          </el-timeline>
        </div>
      </template>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
        <el-button type="primary" @click="handleEdit">
          编辑
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { Document } from '@element-plus/icons-vue'

// Props
interface Props {
  modelValue: boolean
  fundData?: any
  detailType: 'budget' | 'income' | 'expense'
}

const props = withDefaults(defineProps<Props>(), {
  fundData: null,
  detailType: 'budget'
})

// Emits
const emit = defineEmits<{
  'update:modelValue': [value: boolean]
  edit: [data: any]
}>()

// 计算属性
const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

const dialogTitle = computed(() => {
  const typeMap = {
    budget: '预算详情',
    income: '收入详情',
    expense: '支出详情'
  }
  return typeMap[props.detailType]
})

const detailTotal = computed(() => {
  if (!props.fundData?.details) return 0
  return props.fundData.details.reduce((sum: number, item: any) => sum + (item.amount || 0), 0)
})

// 方法
const formatAmount = (amount: number | null | undefined): string => {
  if (amount === null || amount === undefined) return '0.00'
  return amount.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 })
}

// 预算相关方法
const getBudgetTypeText = (type: string) => {
  const typeMap: Record<string, string> = {
    operation: '运营预算',
    maintenance: '维护预算',
    construction: '建设预算',
    emergency: '应急预算'
  }
  return typeMap[type] || type
}

const getBudgetTypeTag = (type: string) => {
  const tagMap: Record<string, string> = {
    operation: 'primary',
    maintenance: 'success',
    construction: 'warning',
    emergency: 'danger'
  }
  return tagMap[type] || 'info'
}

const getBudgetStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    draft: '草稿',
    pending: '待审核',
    approved: '已批准',
    executing: '执行中',
    completed: '已完成'
  }
  return statusMap[status] || status
}

const getBudgetStatusTag = (status: string) => {
  const tagMap: Record<string, string> = {
    draft: 'info',
    pending: 'warning',
    approved: 'success',
    executing: 'primary',
    completed: 'success'
  }
  return tagMap[status] || 'info'
}

// 收入相关方法
const getIncomeTypeText = (type: string) => {
  const typeMap: Record<string, string> = {
    water_fee: '水费收入',
    electricity_fee: '电费收入',
    government_subsidy: '政府补贴',
    other: '其他收入'
  }
  return typeMap[type] || type
}

const getIncomeTypeTag = (type: string) => {
  const tagMap: Record<string, string> = {
    water_fee: 'primary',
    electricity_fee: 'success',
    government_subsidy: 'warning',
    other: 'info'
  }
  return tagMap[type] || 'info'
}

// 支出相关方法
const getExpenseTypeText = (type: string) => {
  const typeMap: Record<string, string> = {
    salary: '人员工资',
    maintenance: '设备维护',
    material: '材料采购',
    utility: '水电费',
    other: '其他支出'
  }
  return typeMap[type] || type
}

const getExpenseTypeTag = (type: string) => {
  const tagMap: Record<string, string> = {
    salary: 'primary',
    maintenance: 'success',
    material: 'warning',
    utility: 'info',
    other: 'info'
  }
  return tagMap[type] || 'info'
}

const getBudgetSubjectText = (subject: string) => {
  const subjectMap: Record<string, string> = {
    operation: '运营费用',
    maintenance: '维护费用',
    personnel: '人员费用',
    equipment: '设备费用'
  }
  return subjectMap[subject] || subject
}

const getApprovalStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    pending: '待审批',
    approved: '已审批',
    rejected: '已拒绝'
  }
  return statusMap[status] || status
}

const getApprovalStatusTag = (status: string) => {
  const tagMap: Record<string, string> = {
    pending: 'warning',
    approved: 'success',
    rejected: 'danger'
  }
  return tagMap[status] || 'info'
}

const downloadFile = (file: any) => {
  // 模拟文件下载
  ElMessage.success(`正在下载文件：${file.name}`)
}

const handleClose = () => {
  visible.value = false
}

const handleEdit = () => {
  emit('edit', props.fundData)
  visible.value = false
}
</script>

<style scoped>
.fund-detail {
  max-height: 600px;
  overflow-y: auto;
}

.detail-section {
  margin-bottom: 24px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 16px 0;
  padding-bottom: 8px;
  border-bottom: 2px solid #e4e7ed;
}

.detail-row {
  margin-bottom: 16px;
}

.detail-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 12px;
}

.detail-item.full-width {
  flex-direction: column;
  align-items: stretch;
}

.label {
  font-weight: 600;
  color: #606266;
  min-width: 100px;
  margin-right: 8px;
}

.value {
  color: #303133;
  flex: 1;
}

.value.amount {
  color: #e6a23c;
  font-weight: 600;
}

.value.description {
  background-color: #f5f7fa;
  padding: 8px 12px;
  border-radius: 4px;
  margin-top: 8px;
  line-height: 1.6;
}

.summary-info {
  margin-top: 16px;
  padding: 16px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.summary-label {
  font-weight: 600;
  color: #606266;
}

.summary-value {
  font-weight: 600;
}

.attachment-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.attachment-item {
  display: flex;
  align-items: center;
  padding: 12px;
  background-color: #f5f7fa;
  border-radius: 4px;
  gap: 12px;
}

.file-icon {
  color: #409eff;
  font-size: 18px;
}

.file-name {
  flex: 1;
  color: #303133;
}

.approval-item {
  padding: 8px 0;
}

.approval-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.approver {
  font-weight: 600;
  color: #303133;
}

.approval-comment {
  color: #606266;
  font-size: 14px;
  line-height: 1.5;
}

.dialog-footer {
  text-align: right;
}

:deep(.el-timeline-item__timestamp) {
  color: #909399;
  font-size: 12px;
}
</style>